package com.hsnn.medstgmini.pro.project.dao.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.pro.project.dao.ProdNoticeDao;
import com.hsnn.medstgmini.pro.project.model.form.ProdNoticeForm;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 生产企业招标公告管理
 * @author 蔡春龙
 * @date 2015年8月24日
 */
@Repository
public class ProdNoticeDaoImpl implements ProdNoticeDao {

	@Resource(name = "sessionFactory")
	private SessionFactory factory;

	@Autowired
	private HibernateSqlUtil sqlUtil;
	
	/**
	 * @category 生产企业分页查询招标公告
	 * @date 2015年8月25日
	 * @param page
	 * @return
	 */
	public Pagination getNoticeList(Pagination page) {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT a.sys_notice_id noticeId, a.sys_notice_code noticeCode, a.title, a.org, a.content, DATE_FORMAT(a.datetime, '%Y-%m-%d') datetime, a.url, a.read_count readCount, b.proj_id projId, c.name projName FROM sys_notice a JOIN bid_projsysnotice b ON a.sys_notice_id = b.sys_notice_id JOIN bid_project c ON b.proj_id = c.proj_id WHERE a.status = :status AND b.status = :status ");
		
		ProdNoticeForm form = (ProdNoticeForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Status.enabled.getKey());
		
		if (form != null) {
			String noticeTitle = form.getNoticeTitle();
			if (StringUtils.isNotBlank(noticeTitle)) {
				sb.append(" AND a.title LIKE :noticeTitle");
				params.put("noticeTitle", "%" + noticeTitle + "%");
			}
			
			String startDate = form.getStartDate();
			if (StringUtils.isNotBlank(startDate)) {
				sb.append(" AND DATE_FORMAT(a.datetime, '%Y-%m-%d') >= :startDate");
				params.put("startDate", startDate);
			}
			
			String endDate = form.getEndDate();
			if (StringUtils.isNotBlank(endDate)) {
				sb.append(" AND DATE_FORMAT(a.datetime, '%Y-%m-%d') <= :endDate");
				params.put("endDate", endDate);
			}
		}
		
		sb.append(" ORDER BY a.datetime DESC ");
		
		return sqlUtil.getSqlPageMapList(sb.toString(), params, page);
	}
	
}
